package com.diaohw.platform.framework.rabbit;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class RabbitMQConfig {


    public RabbitTemplate rabbitTemplate(CachingConnectionFactory factory) {
        factory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);

        factory.setPublisherReturns(true);

        RabbitTemplate rabbitTemplate = new RabbitTemplate();

        rabbitTemplate.setMandatory(true);

        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());

        rabbitTemplate.setConfirmCallback(((correlationData, ack, cause) -> {

        }));

        rabbitTemplate.setReturnsCallback((returnedMessage -> {
            log.info("消息确认");
        }));

        rabbitTemplate.setRecoveryCallback(context -> {

            return context;
        });


        return rabbitTemplate;
    }
}
